﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class CreateEzEventCenterScripts : Editor {

    static string strCallBack =     @"Assets\EzFreamWork\Editor\EventCallBackTxt\ExampleTxt\CallBack.txt";
    static string strEventCenter =  @"Assets\EzFreamWork\Editor\EventCallBackTxt\ExampleTxt\EventCenter.txt";
    static string strEzEventType =  @"Assets\EzFreamWork\Editor\EventCallBackTxt\ExampleTxt\EzEventType.txt";

    [MenuItem("EzFreamWork/CreateEzEventCenter")]
    static void AutoCreateScript()
    {
        TextAsset CallBack = AssetDatabase.LoadAssetAtPath<TextAsset>(strCallBack);
        TextAsset EventCenter = AssetDatabase.LoadAssetAtPath<TextAsset>(strEventCenter);
        TextAsset EzEventType = AssetDatabase.LoadAssetAtPath<TextAsset>(strEzEventType);

        if (File.Exists(@"Assets\EzFreamWork\Editor\EventCallBackTxt\CallBack.cs"))
        {
            WriteStrToFile(CallBack.text, @"Assets\EzFreamWork\Editor\EventCallBackTxt\", "CallBack.cs");
            Debug.Log("生成"+ CallBack.text);
        }
        else
        {
            Debug.Log("无需再生成");
        }

        if (File.Exists(@"Assets\EzFreamWork\Editor\EventCallBackTxt\EventCenter.cs"))
        {
            WriteStrToFile(EventCenter.text, @"Assets\EzFreamWork\Editor\EventCallBackTxt\", "EventCenter.cs");
            Debug.Log("生成" + EventCenter.text);
        }
        else
        {
            Debug.Log("无需再生成");
        }

        if (File.Exists(@"Assets\EzFreamWork\Editor\EventCallBackTxt\EzEventType.cs"))
        {
            WriteStrToFile(EzEventType.text, @"Assets\EzFreamWork\Editor\EventCallBackTxt\", "EzEventType.cs");
            Debug.Log("生成" + EzEventType.text);
        }
        else
        {
            Debug.Log("无需再生成");
        }

        AssetDatabase.Refresh();
    }


    //写入文件
    static void WriteStrToFile(string txt, string path, string fileName)
    {
        if (Directory.Exists(path) == false)
        {
            Directory.CreateDirectory(path);
        }
        if (string.IsNullOrEmpty(txt) || string.IsNullOrEmpty(path))
        {
            return;
        }

        string tempPath = path + "/" + fileName + ".cs";
        if (File.Exists(tempPath))
        {
            File.Delete(tempPath);
        }
        File.WriteAllText(path + "/" + fileName + ".cs", txt, Encoding.UTF8);
    }
}
